package com.estate.web.controller.eastate;

import com.estate.common.annotation.Log;
import com.estate.common.constant.UserConstants;
import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.page.TableDataInfo;
import com.estate.common.enums.BusinessType;
import com.estate.common.utils.poi.ExcelUtil;
import com.estate.system.domain.SysAdvantage;
import com.estate.system.domain.SysPost;
import com.estate.system.service.ISysAdvantageService;
import com.estate.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优点标签信息操作处理
 *
 * @author estate
 */
@Controller
@RequestMapping("/system/advantage")
@CrossOrigin
public class SysAdvantageController extends BaseController
{
    private String prefix = "system/advantage";

    @Autowired
    private ISysAdvantageService advantageService;

    @RequiresPermissions("system:advantage:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/advantage";
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAdvantage advantage)
    {
        startPage();
        List<SysAdvantage> list = advantageService.selectAdvantageList(advantage);
        return getDataTable(list);
    }
    /*
    *  查询优点标签
    * */

    @PostMapping("/selectAdvantageById")
    @ResponseBody
    public AjaxResult SelectAdvantageById(long id)
    {
        SysAdvantage advantage1 = advantageService.selectAdvantageById(id);


        return new AjaxResult().put("charName",advantage1.getAdvantageName());
    }

    @RequiresPermissions("system:advantage:list")
    @PostMapping("/listall")
    @ResponseBody
    public List<SysAdvantage> listbyid()
    {

        return advantageService.selectAdvantageAll();
    }

    @Log(title = "优点标签管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:advantage:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAdvantage advantage)
    {
        List<SysAdvantage> list = advantageService.selectAdvantageList(advantage);
        ExcelUtil<SysAdvantage> util = new ExcelUtil<SysAdvantage>(SysAdvantage.class);
        return util.exportExcel(list, "优点数据");
    }

    @RequiresPermissions("system:advantage:remove")
    @Log(title = "优点管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(advantageService.deleteAdvantageByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增优点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优点
     */
    @RequiresPermissions("system:advantage:add")
    @Log(title = "优点管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysAdvantage advantage)
    {
        if (UserConstants.ADVANTAGE_NAME_NOT_UNIQUE.equals(advantageService.checkAdvantageNameUnique(advantage)))
        {
            return error("新增优点'" + advantage.getAdvantageName() + "'失败，优点名称已存在");
        }

        advantage.setCreateBy(getLoginName());
        return toAjax(advantageService.insertAdvantage(advantage));
    }

    /**
     * 修改优点
     */
    @RequiresPermissions("system:advantage:edit")
    @GetMapping("/edit/{advantageId}")
    public String edit(@PathVariable("advantageId") Long advantageId, ModelMap mmap)
    {
        mmap.put("advantage", advantageService.selectAdvantageById(advantageId));
        return prefix + "/edit";
    }

    /**
     * 修改保存优点
     */
    @RequiresPermissions("system:advantage:edit")
    @Log(title = "优点管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysAdvantage advantage)
    {
        if (UserConstants.ADVANTAGE_NAME_NOT_UNIQUE.equals(advantageService.checkAdvantageNameUnique(advantage)))
        {
            return error("修改优点'" + advantage.getAdvantageName() + "'失败，优点名称已存在");
        }

        advantage.setUpdateBy(getLoginName());
        return toAjax(advantageService.updateAdvantage(advantage));
    }

    /**
     * 校验优点名称
     */
    @PostMapping("/checkAdvantageNameUnique")
    @ResponseBody
    public String checkAdvantageNameUnique(SysAdvantage advantage)
    {
        return advantageService.checkAdvantageNameUnique(advantage);
    }


}
